package com.idlefish.flutterboost.containers;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.idlefish.flutterboost.CallBack;
import com.idlefish.flutterboost.EngineBindings;
import com.idlefish.flutterboost.FlutterPayManager;
import com.idlefish.router_plugin.R;

import io.flutter.embedding.android.FlutterFragmentActivity;
import io.flutter.embedding.engine.FlutterEngine;

import static com.idlefish.flutterboost.containers.FlutterActivityLaunchConfigs.EXTRA_PARAMS;

public class SingleOnceFlutterActivity extends FlutterFragmentActivity {
    private static final String TAG = SingleOnceFlutterActivity.class.getSimpleName();

    private EngineBindings engineBindings;

    private CallBack<String> callBack = new CallBack<String>() {
        @Override
        public void onCall(String status) {
            switch (status) {
                case FlutterPayManager.PAY_STATUS_SUCCESS:
                    if (engineBindings != null) {
                        engineBindings.invokeMethod("payNotify", "{status=1,from=" + FlutterPayManager.getInstance().getFlutterForm() + "}");
                    }
                    break;
                case FlutterPayManager.PAY_STATUS_FAIL:
                    if (engineBindings != null) {
                        engineBindings.invokeMethod("payNotify", "{status=2,from=" + FlutterPayManager.getInstance().getFlutterForm() + "}");
                    }
                    break;
                case FlutterPayManager.PAY_STATUS_CONFIRM:
                    break;
            }
        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        try {
            String entryPoint = getIntent().getStringExtra(EXTRA_PARAMS);
            Log.e(TAG, entryPoint);
            if (TextUtils.isEmpty(entryPoint)) {
                super.onCreate(savedInstanceState);
                return;
            }
            JsonElement jsonElement = JsonParser.parseString(entryPoint);
            if (jsonElement.isJsonObject()) {
                JsonObject jsonObject = jsonElement.getAsJsonObject();
                if (jsonObject.has("routeName")) {
                    engineBindings = new EngineBindings(this, jsonObject.get("routeName").getAsString(), entryPoint);
                }
            }
        } catch (Exception e) {
            Log.e("Exception", e.toString());
        } finally {
            super.onCreate(savedInstanceState);
            launchAnim(true);//加载动画
            FlutterPayManager.getInstance().register(callBack);
        }
    }

    public void launchAnim(boolean open) {
        if (open) {
            overridePendingTransition(R.anim.activity_left_open, R.anim.activity_keep);
        } else {
            overridePendingTransition(R.anim.activity_keep, R.anim.activity_left_close);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (engineBindings != null) {
            engineBindings.detach();
        }
        launchAnim(false);
        FlutterPayManager.getInstance().unRegister(callBack);
    }


    @Nullable
    @Override
    public FlutterEngine provideFlutterEngine(@NonNull Context context) {
        if (engineBindings != null) {
            return engineBindings.getEngine();
        }
        return super.provideFlutterEngine(context);
    }
}
